package tw.com.hitevision.whiteboard.android.stateFile;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import java.util.List;

public class JsonHandler extends Handler {
    private static final String TAG = JsonHandler.class.getSimpleName();

    private static final int MSG_WHAT_LOAD_JSON_TO_STATE = 5;
    private static final int MSG_WHAT_SAVE_JSON_TO_FILE = 6;
    private static final int MSG_WHAT_DELETE_JSON = 7;
    private static final int MSG_WHAT_COPY_JSON = 8;
    private static final int MSG_WHAT_SAVE_TO_JSON_IMAGE_INTERNAL = 9;

    private static final String DATA_IMAGE = "dataImage";
    private static final String DATA_FILE_NAME = "dataFileName";
    private static final String DATA_ARRAY = "dataArray";
    private static final String DATA = "data";

    private StateFileSaver stateFileSaver;

    public JsonHandler(Context context, Looper looper) {
        super(looper);

        stateFileSaver = StateFileSaver.getInstance(context);
    }

    public void sendLoadJsonToStateMessage() {
        final Message msg = Message.obtain(this, MSG_WHAT_LOAD_JSON_TO_STATE);

        sendMessage(msg);
    }

    public void sendSaveJsonMessage(String fileName, WhiteboardStateFile whiteboardStateFile) {
        if (fileName != null && whiteboardStateFile != null) {
            final Bundle b = new Bundle();
            b.putString(DATA_FILE_NAME, fileName);
            b.putParcelable(DATA, whiteboardStateFile);

            final Message msg = Message.obtain(this, MSG_WHAT_SAVE_JSON_TO_FILE);
            msg.setData(b);

            sendMessage(msg);
        }
    }

    public void sendDeleteJsonFileMessage(List<Integer> ids, int size) {
        sendJsonFileMessage(ids, size, MSG_WHAT_DELETE_JSON);
    }

    public void sendCopyJsonFileMessage(List<Integer> ids, int size) {
        sendJsonFileMessage(ids, size, MSG_WHAT_COPY_JSON);
    }

    private void sendJsonFileMessage(List<Integer> ids, int size, int key) {
        if (ids.size() > 0) {
            final Bundle b = new Bundle();

            int[] ints = new int[ids.size()];
            for (int i = 0;i< ids.size();i++) {
                ints[i] = ids.get(i);
            }

            b.putIntArray(DATA_ARRAY, ints);
            b.putInt(DATA, size);

            final Message msg = Message.obtain(this, key);
            msg.setData(b);

            sendMessage(msg);
        }
    }

    public void sendSaveJsonImageToInternal(Bitmap bitmap, String fileName) {
        if (bitmap != null) {
            final Bundle b = new Bundle();
            b.putParcelable(DATA_IMAGE, bitmap);
            b.putString(DATA_FILE_NAME, fileName);

            final Message msg = Message.obtain(this, MSG_WHAT_SAVE_TO_JSON_IMAGE_INTERNAL);
            msg.setData(b);

            sendMessage(msg);
        }
    }

    @Override
    public void handleMessage(Message msg) {
//        super.handleMessage(msg);
        switch (msg.what) {
            case MSG_WHAT_LOAD_JSON_TO_STATE: {
                stateFileSaver.loadJsonToStateFile();

                break;
            }
            case MSG_WHAT_SAVE_JSON_TO_FILE: {
                final Bundle b = msg.getData();
                final String fileName = b.getString(DATA_FILE_NAME);
                final WhiteboardStateFile whiteboardStateFile = b.getParcelable(DATA);

                if (fileName != null && whiteboardStateFile != null) {
                    stateFileSaver.saveJsonToFile(fileName, whiteboardStateFile);
                }

                break;
            }
            case MSG_WHAT_DELETE_JSON: {
                final Bundle b = msg.getData();
                final int[] ids = b.getIntArray(DATA_ARRAY);
                final int size = b.getInt(DATA);

                if (ids != null && ids.length > 0) {
                    stateFileSaver.deleteJsonFiles(ids, size);
                }

                break;
            }
            case MSG_WHAT_COPY_JSON: {
                final Bundle b = msg.getData();
                final int[] ids = b.getIntArray(DATA_ARRAY);
                final int size = b.getInt(DATA);

                if (ids != null && ids.length > 0) {
                    stateFileSaver.copyJsonFiles(ids, size);
                }

                break;
            }
            case MSG_WHAT_SAVE_TO_JSON_IMAGE_INTERNAL: {
                final Bundle b = msg.getData();
                final Bitmap image = b.getParcelable(DATA_IMAGE);
                final String fileName = b.getString(DATA_FILE_NAME);

                if (image != null) {
                    stateFileSaver.saveJsonImageToInternalStorage(image, fileName);
                }

                break;
            }
        }
    }

    private void log(String s) {
        Log.d(TAG, s);
    }

}
